package com.cms.server.resolver.plugins.scsq;

import java.sql.ResultSet;
import java.util.Date;

import com.dal.common.sequence.UUIDGenerator;
import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.FormCodeUtil;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

public class CMSSubmitSCSQToSCTZ extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void finishSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = model.getBillDataSet();
		EFRowSet                      headRowset = EFRowSet.getInstance();
		EFRowSet                      nodeRowset = EFRowSet.getInstance();
		String                            strSql = "";
		ResultSet                             rs = null;
		JParamObject                      sctzPO = JParamObject.Create();
		String                            F_KJQJ = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
		String                            F_GUID = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
		String                            F_DJBH = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
		String                           OP_BMBH = PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "");
		String                           OP_USER = PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "");
		String                      OP_USER_NAME = PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "");
		String                              code = "";
		long                             curTime = new Date().getTime();
		String                         SCTZ_GUID = UUIDGenerator.nextId();
		String                           NODE_ID = PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "");
		String                     RESR_IN_CAUSE = PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "");
		String                          sctzDjbh = "";
		
		if(!PO.GetValueByParamName("FLOW_ID", "").equals("SCSQ_FLOW")) return;
		
		strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='SCSQ_FLOW' and NODE_ID = '" + NODE_ID + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
		
		if(!nodeRowset.getString("NODE_TYPE", "").equals("end") || !RESR_IN_CAUSE.equals("submit")) return;
		
		strSql = "select * from SCSQXX where F_KJQJ = '" + F_KJQJ + "' and F_GUID = '" + F_GUID + "' and F_DJBH = '" + F_DJBH + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) headRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, headRowset);
		sctzPO.SetValueByParamName(SYS_MODEL.MODEL_ID, "SCTZModel");
		
		//判断生产通知单是否存在
		sctzDjbh = "SCTZ" + F_DJBH.substring(4);
		strSql = "select * from SCTZXX where F_KJQJ = '" + F_KJQJ + "' and F_DJBH = '" + sctzDjbh + "'";
		rs = stmt.executeQuery(strSql);
		//如果通知单已经存在，则用一个新的编号
		if(rs != null && rs.next()) {
			code = FormCodeUtil.GetNewBillCode(stmt, sctzPO, headRowset);
		}
		//否则用申请单的号，只是前缀修改
		else {
			code = sctzDjbh;
		}

		strSql = "insert into SCTZXX (F_DJBH,F_KJQJ,F_CODE,F_GUID,F_ZDR,F_ZDRQ,F_HTBH,F_XMBH,F_CHDATE,F_CRDATE,F_NOTE,F_XMFZR,F_YWFZR," 
			   + "F_JSQD,F_SFCQ,F_WGRQ,F_JSFZR,SCSQ_KJQJ,SCSQ_DJBH,SCSQ_GUID,F_DJZT,F_HTQK,F_JHQK,F_ZJLY) select "
			   + "'" + code + "',F_KJQJ,'" + OP_BMBH + "','" + SCTZ_GUID + "','" + OP_USER + "',F_ZDRQ,F_HTBH,F_XMBH,F_CHDATE,F_CRDATE,F_NOTE,F_XMFZR,F_YWFZR,"
			   + "F_JSQD,F_SFCQ,F_WGRQ,F_JSFZR,'" + F_KJQJ + "','" + F_DJBH + "','" + F_GUID + "', '1',F_HTQK,F_JHQK,F_ZJLY from SCSQXX where F_DJBH = '" + F_DJBH + "' and F_KJQJ = '"
		       + F_KJQJ + "' and F_GUID = '" + F_GUID + "'";
		stmt.execute(strSql);
		
		strSql = "insert into SCTZMX (F_DJBH,F_KJQJ,F_GUID,F_FLBH,F_XMBH,F_CPBH,F_SCSL,F_CHDATE,F_CRDATE) select "
			   + "'" + code + "',F_KJQJ,'" + SCTZ_GUID + "',F_FLBH,F_XMBH,F_CPBH,F_SCSL,F_CHDATE,F_CRDATE from SCSQMX  where F_DJBH = '" + F_DJBH + "' and F_KJQJ = '"
		       + F_KJQJ + "' and F_GUID = '" + F_GUID + "'";
		stmt.execute(strSql);
		
		//更新上一个任务节点的状态
		strSql = "update SCSQXX set SCTZ_KJQJ = '" + F_KJQJ + "', SCTZ_DJBH = '" + code + "', SCTZ_GUID = '" + F_GUID + "'"
			   + " where F_KJQJ = '" + F_KJQJ + "' and F_GUID = '" + SCTZ_GUID + "' and F_DJBH = '" + F_DJBH + "'";
		stmt.execute(strSql);
		
		//生产通知单流程的第一个节点
		strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='SCTZ_FLOW' and NODE_ID = 'N1'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
		
		//开始节点单独处理，保存后就要进入流程任务列表，只要保存一次，以后就不再更新
		if(rs == null || !rs.next()) {
			strSql = "insert into FLOW_TASK_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,"
				   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS,TASK_TO_UNIT,TASK_TO_UNIT_NAME," ;
		 	
			for(int j = 1; j <= 20; j++) {
				if(j < 10) {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
						strSql += "F_CHAR0" + j + ",";
					}
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
						strSql += "F_DATE0" + j + ",";
					}
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
						strSql += "F_NUM0" + j + ",";
					}
				} else {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
						strSql += "F_CHAR" + j + ",";
					}
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) != null) {
						strSql += "F_DATE" + j + ",";
					}
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) != null) {
						strSql += "F_NUM" + j + ",";
					}
				}
			}
		 	strSql += "RESR_IN_CAUSE,RESR_OUT_CAUSE) values ('"
		 	        + curTime + "', 'SCTZ_FLOW', '"
					+ code + "', '" + F_KJQJ + "', '" + SCTZ_GUID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "")
					+ "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "") + "', '"
					+ curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "") + "', '" 
					+ headRowset.getString("F_KJQJ", "") + "-" + headRowset.getString("F_DJBH", "") + "生产申请单自动生成生产通知单', 'processing', 'N1', 'N1', '"
					+ nodeRowset.getString("NODE_NAME", "") + "', 'N1', '" + nodeRowset.getString("NODE_NAME", "") 
					+ "', '', 'pending','','',";
		 	for(int j = 1; j <= 20; j++) {
		 		if(j < 10) {
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null)  + "',";
		 			} 
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null)  + "',";
		 			}
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null)  + "',";
		 			}
		 		} else {
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null)  + "',";
		 			} 
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null)  + "',";
		 			}
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null)  + "',";
		 			}
		 		}
		 	}
		 	strSql += "'create', '')";
			stmt.execute(strSql);
		}
	}
}
